using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace OdsControllerLib {
	[XmlRootAttribute("ODSControllerSettings")]
	public class OdsSettingsObj {
		#region fields
		string _settingsFile;
		// UPDATE
		int _updateInterval=30;
		OdsUpdateMethod _updateMethod = OdsUpdateMethod.Manual;
		bool _updateOnIPChange=true;
		
		// LOG
		string _logFilename;
		OdsLogLevel _logDetail = OdsLogLevel.Minimal;
		OdsLogLevel _logLevel = OdsLogLevel.Normal;

		//SERVER
		string _serverPassword = "";
		int _serverPortNumber = 7070;
		string _serverAddress = "update.ods.org";
		string _serverUsername = "";

		// PubIP detect
		string _pubIPCheckUrl = @"http://checkip.dyndns.org:8245/";
		string _pubIPCheckRegEx = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
		#endregion fields

		#region Properties
		/// <summary>
		/// the path and name of the settings xml file to read from and save to
		/// </summary>
		public string SettingsFile {
			get { return _settingsFile; }
			set { _settingsFile = value; }
		}
		// Update
		/// <summary>
		/// Interval in minutes for the app timer or service timer to control and update active records
		/// </summary>
		[XmlElement("UpdateInterval")]
		public int UpdateInterval {
			get { return _updateInterval; }
			set { _updateInterval = value; }
		}
		/// <summary>
		/// Defines how the application starts the timed update routine, it doesn't (manual), from the app (it must be running) or the windows service (must be installed and running)
		/// </summary>
		[XmlElement("UpdateMethod")]
		public OdsUpdateMethod UpdateMethod {
			get { return _updateMethod; }
			set { _updateMethod = value; }
		}
		[XmlElement("UpdateOnIPChange")]
		public bool UpdateOnIPChange {
			get { return _updateOnIPChange; }
			set { _updateOnIPChange = value; }
		}
		// LOG
		/// <summary>
		/// The amount of information to write to log
		/// </summary>
		[XmlElement("LogLevel")]
		public OdsLogLevel LogLevel {
			get { return _logLevel; }
			set { _logLevel = value; }
		}
		/// <summary>
		/// The relative path of the txt log file (relative to the app start path)
		/// </summary>
		[XmlElement("LogFilename")]
		public string LogFilename {
			get { return _logFilename; }
			set { _logFilename = value; }
		}

		// SERVER
		/// <summary>
		/// The IP or dns address of the update server (normaly: update.ods.org)
		/// </summary>
		[XmlElement("ServerAddress")]
		public string ServerAddress {
			get { return _serverAddress; }
			set { _serverAddress = value; }
		}
		/// <summary>
		/// The TCP port number on whitch the server runs
		/// </summary>
		[XmlElement("ServerPortNumber")]
		public int ServerPortNumber {
			get { return _serverPortNumber; }
			set { _serverPortNumber = value; }
		}
		/// <summary>
		/// The user's login/username at the ODS service
		/// </summary>
		[XmlElement("ServerUsername")]
		public string ServerUsername {
			get { return _serverUsername; }
			set { _serverUsername = value; }
		}
		/// <summary>
		/// The user's password at the ODS service
		/// </summary>
		[XmlElement("ServerPassword")]
		public string ServerPassword {
			get { return _serverPassword; }
			set { _serverPassword = value; }
		}
		//PubIP detect
		/// <summary>
		/// The URL that returns the connections public IP
		/// </summary>
		[XmlElement("PubIPCheckURL")]
		public string PubIPCheckUrl {
			get { return _pubIPCheckUrl; }
			set { _pubIPCheckUrl = value; }
		}
		/// <summary>
		/// To match the public ip from the full web response
		/// </summary>
		[XmlElement("PubIPCheckRegEx")]
		public string PubIPCheckRegEx {
			get { return _pubIPCheckRegEx; }
			set { _pubIPCheckRegEx = value; }
		}

		#endregion Properties
		
	
		public bool LoadFromXML() {
			if (string.IsNullOrEmpty(_settingsFile))
				throw new Exception("UnInitialized settings file");

			try {
				if (!File.Exists(_settingsFile)) return false;

				XmlSerializer serializer = new XmlSerializer(typeof(OdsSettingsObj));
				FileStream fs = new FileStream(_settingsFile, FileMode.Open);
				OdsSettingsObj loaded_settings;
				loaded_settings = (OdsSettingsObj)serializer.Deserialize(fs);
				fs.Close();
				//log
				this._logDetail = loaded_settings._logDetail;
				this._logFilename = loaded_settings._logFilename;
				this._logLevel = loaded_settings._logLevel;
				//server
				this._serverAddress = loaded_settings._serverAddress;
				this._serverPassword = loaded_settings._serverPassword;
				this._serverPortNumber = loaded_settings._serverPortNumber;
				this._serverUsername = loaded_settings._serverUsername;
				//UpdateCycle
				this._updateInterval = loaded_settings._updateInterval;
				this._updateMethod = loaded_settings._updateMethod;
				this._updateOnIPChange = loaded_settings._updateOnIPChange;
				//PubIP
				this._pubIPCheckUrl = loaded_settings._pubIPCheckUrl;
				this._pubIPCheckRegEx = loaded_settings._pubIPCheckRegEx;
				return true;
			} catch { return false; }
		}

		public bool SaveToXML() {
			if (string.IsNullOrEmpty(_settingsFile))
				throw new Exception("UnInitialized settings file");

			System.Diagnostics.Debug.WriteLine("main_ods.SaveXMLConfig - configfile: " + _settingsFile);
			try {
				XmlSerializer serializer = new XmlSerializer(typeof(OdsSettingsObj));
				TextWriter writer = new StreamWriter(_settingsFile);
				serializer.Serialize(writer, this);
				writer.Close();
			} catch {
				return false;
			}
			return true;
		}

      
	}
}
